package com.lht.testDemo;

import org.junit.Test;
import sun.misc.Unsafe;

import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author Eric
 * @version 1.0
 * @date 2019-07-10
 */
public class EnumTest {


    @Test
    public void stringBuilder() {
        int len = 1000;
        int[] arr = new int[len];
        Random random = new Random();
        for (int i = 0; i < arr.length; i++) {
            arr[i] = random.nextInt(1000);
        }

        String s = "";
        Long start = System.nanoTime();
        for (int i = 0; i < arr.length; i++) {
            s += arr[i];
        }
        Long end = System.nanoTime();
        Long count = end - start;
        System.out.println("+号耗时：" + count);

        start = System.nanoTime();
        String guess = "";
        for (int i = 0; i < arr.length; i++) {
            StringBuilder sb = new StringBuilder(guess);
            guess = sb.append(arr[i]).toString();
        }
        end = System.nanoTime();
        count = end - start;
        System.out.println("自己append耗时：" + count);

        start = System.nanoTime();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < arr.length; i++) {
            sb.append(arr[i]);
        }
        end = System.nanoTime();
        count = end - start;
        System.out.println("append耗时：" + count);
    }

    @Test
    public void strToEnum() {
        System.out.println('0'+1-1);
        System.out.println('9'+1-1);
        String xiaoming = "XIAOMING";
        A a = A.valueOf(xiaoming);
        System.out.println(a.getName() + "," + a.getAge());
    }

    @Test
    public void intToEnum() {
        int i = 0;
        A a = A.values()[i];
        System.out.println(a.getName() + "," + a.getAge());
    }

    @Test
    public void enumToString() {
        String name = A.XIAOMING.name();
        System.out.println(name);
    }

    @Test
    public void enumOrdinal(){
        for(A a : A.values()){
            System.out.println("枚举名："+ a.name()+", 枚举下标："+ a.ordinal());
        }
    }
    enum A {
        XIAOHUA("xiaohua", 20),
        XIAOMING("xiaoming", 20);

        private String name;
        private Integer age;

        A(String name, Integer age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }
    }
}


//class SafePage {
//
//    private static AtomicInteger pageIndex = new AtomicInteger(0);
//
//    private static  int index = 0;
//
//    Unsafe unsafe= Unsafe.getUnsafe();
//
//    public static  int obtainIndex() {
////        return pageIndex.incrementAndGet();
//        return ++index;
//    }
//
//    private static final int DEFAULT_SIZE = 10;
//    private static CountDownLatch lock = new CountDownLatch(10);
//
//
//    public static void main(String[] args) {
//        for (int i = 0; i < 10; i++) {
//            Thread run = new Thread(new Runnable() {
//
//                @Override
//                public void run() {
//                    lock.countDown();
//                    try {
//                        Thread.sleep(1000);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                    lock.countDown();
//                    int index = obtainIndex();
//                    int start = (index - 1) * DEFAULT_SIZE;
//                    int end = index * DEFAULT_SIZE;
//                    System.out.println("ThreadName;" + Thread.currentThread().getName() + ",开始位置：" + start + ",结束位置：" + end);
//                }
//            });
//            run.start();
//        }
//        try {
//            lock.await();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        System.out.println("主线程执行了");
//    }
//}
